home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / allison / ddir2.cpp < prev    next >
C/C++ Source or Header  |  1994-05-02  |  3KB  |  123 lines

  1. LISTING 4 - Illustrates Exception Classes
  2.  
  3. // ddir2.cpp
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <io.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. #include <dirent.h>
  10. #include <dir.h>
  11. #include <iostream.h>
  12. #include <fstream.h>
  13. #include <strstream.h>
  14.  
  15. // Exception classes
  16. class Dir_err
  17. {};
  18. class Bad_dir : public Dir_err
  19. {};
  20. class Dir_open_err : public Dir_err
  21. {};
  22. class File_del_err : public Dir_err
  23. {};
  24. class Dir_del_err : public Dir_err
  25. {};
  26.  
  27. // Put response file in root directory
  28. char response_file[L_tmpnam+1] = "\\";
  29. // (Change this to "/" for UNIX)
  30.  
  31.  
  32. main(int argc, char **argv)
  33. {
  34.     char *old_path = getcwd(NULL,MAXDIR);
  35.     void rd(char *);
  36.  
  37.     // Initialize response file for DOS "del" command
  38.     tmpnam(response_file+1);
  39.     ofstream(response_file) << "Y\n";
  40.  
  41.     // Delete the directories
  42.     while (--argc)
  43.     {
  44.         try
  45.         {
  46.             rd(*++argv);
  47.         }
  48.  
  49.         // Catch exceptions
  50.         catch(Bad_dir)
  51.         {
  52.             cerr << "Invalid directory\n";
  53.         }
  54.         catch(Dir_open_err)
  55.         {
  56.             cerr << "Error opening directory\n";
  57.         }
  58.         catch(File_del_err)
  59.         {
  60.             cerr << "Error deleting file\n";
  61.         }
  62.         catch(Dir_del_err)
  63.         {
  64.             cerr << "Error deleting directory\n";
  65.         }
  66.         catch(Dir_err)
  67.         {
  68.             cerr << "Directory error\n";
  69.         }
  70.     }
  71.  
  72.     // Clean-up
  73.     remove(response_file);
  74.     chdir(old_path);
  75.     return 0;
  76. }
  77.  
  78. void rd(char* dir)
  79. {
  80.     // Log onto the directory that is to be deleted
  81.     cout << strlwr(dir) << endl;
  82.     if (chdir(dir))
  83.         throw Bad_dir();
  84.  
  85.     // Delete all normal files via OS shell
  86.     const size_t SH_CMD_LEN = 14;
  87.     char sh_cmd[L_tmpnam+SH_CMD_LEN+1];
  88.     ostrstream(sh_cmd,sizeof sh_cmd) << "del *.* <"
  89.                                      << response_file
  90.                                      << " >nul"
  91.                                      << ends;
  92.     system(sh_cmd);
  93.  
  94.     // Delete any remaining directory entries
  95.     DIR *dirp;
  96.     struct dirent *entry;
  97.     if ((dirp = opendir(".")) == NULL)
  98.         throw Dir_open_err();
  99.     while ((entry = readdir(dirp)) != NULL)
  100.     {
  101.         struct stat finfo;
  102.  
  103.         if (entry->d_name[0] == '.')
  104.             continue;
  105.         stat(entry->d_name,&finfo);
  106.         if (finfo.st_mode & S_IFDIR)
  107.             rd(entry->d_name);      // Subdirectory
  108.         else
  109.         {
  110.             // Enable delete of file, then do it
  111.             chmod(entry->d_name,S_IWRITE);
  112.             if (unlink(entry->d_name))
  113.                 throw File_del_err();
  114.         }
  115.     }
  116.     closedir(dirp);
  117.  
  118.     // Remove the directory from its parent
  119.     chdir("..");
  120.     if (rmdir(dir))
  121.         throw Dir_del_err();
  122. }
  123.